<template>
	<view class="container">
		<view class="group-box wrap">
			<uni-swipe-action class="address-list">
				<!--循环.开始-->
				<uni-swipe-action-item v-for="(item,index) in listData" :key="index">
					<view class="list-box">
						<view class="left" @click="checkAddress(item)">
							<view class="title">
								<text v-if="item.isDefault" class="tag">默认</text>
								<text>{{item.title}}</text>
							</view>
							<view class="detail">
								<text class="name">{{item.invoiceType}}</text>
								<text class="tel">{{item.dutyParagraph}}</text>
							</view>
						</view>
						<view class="right">
							<navigator :url="'edit?id='+item.id" class="icon iconfont icon-edit"></navigator>
						</view>
					</view>
					<template v-slot:right>
						<view @click="deleteItem(item.id)" class="list-btn">
							<text class="icon iconfont icon-delete"></text>
						</view>
					</template>
				</uni-swipe-action-item>
				<!--循环.结束-->
			</uni-swipe-action>
			<uni-load-more :status="loadingType" @clickLoadMore="loadData('add')"></uni-load-more>
		</view>
		<view class="foot-wrap">
			<view class="foot-box">
				<navigator url="edit" class="btn-add">新增发票</navigator>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				source: 0, //页面回传标记
				loadingType: "more", //加载状态
				totalCount: 0, //总数量
				totalPages: 0, //总页数
				pageSize: 10, //每页显示条数
				pageIndex: 1, //当前页码
				listData: [], //地址列表
			}
		},
		methods: {
			//初始化数据,带下滑刷新和上滑加载
			async loadData(type='add', loading){
				let _this = this;
				//上滑加载更多
				if(type === 'add'){
					if(_this.loadingType === 'nomore'){
						return;
					}
					_this.loadingType = 'loading';
				}
				//下滑刷新
				else{
					_this.pageIndex=1;
					_this.listData=[];
					_this.loadingType="more";
				}
				
				//加载地址列表
				await _this.$api.request({
					url: `/account/member/invoice/list?pageSize=${_this.pageSize}&pageIndex=${_this.pageIndex}`,
					loading: true
				}).then(res=>{
					let pageInfo = JSON.parse(res.header["x-pagination"]);
					_this.totalCount = pageInfo.totalCount;
					_this.totalPages = pageInfo.totalPages;
					_this.listData = _this.listData.concat(res.data);
				}).catch((err)=>{
					_this.loadingType="nomore";
					uni.stopPullDownRefresh();
				});
				
				//判断是否还有下一页
				if(_this.pageIndex >= _this.totalPages){
					_this.loadingType="nomore";
				}else{
					_this.pageIndex++;
					_this.loadingType="more";
				}
				//下滑隐藏动画及上滑事件
				if(type === 'refresh'){
					if(loading == 1){
						uni.hideLoading()
					}else{
						uni.stopPullDownRefresh();
					}
				}
			},
			//选择地址
			checkAddress(item){
				if(this.source == 1){
					//this.$api.prePage()获取上一页实例，在App.vue定义
					this.$api.prePage().useAddress = item;
					this.$api.prePage().useDelivery = {};
					uni.navigateBack();
				}
			},
			//添加或修改成功之后回调
			refreshList(data){
				this.loadData('refresh');
			},
			//删除地址
			async deleteItem(id){
				let _this=this;
				await _this.$api.request({
					url: `/account/member/invoice/${id}`,
					method: "delete",
					loading: true
				}).then(res=>{
					this.loadData('refresh');
				});
			}
		},
		//页面初始化
		onLoad(options){
			this.source = options.source;
			this.loadData('add');
		},
		//下滑刷新
		onPullDownRefresh(){
			this.loadData('refresh');
		},
		//上滑加载更多
		onReachBottom(){
			this.loadData('add');
		}
	}
</script>

<style lang="scss">
	.address-list{
		border-radius: 20rpx;
		overflow: hidden;
		.list-btn{
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-bottom: 4rpx;
			padding: 0 30rpx;
			background: #ff0000;
			.icon{
				color: #fff;
				font-size: 40rpx;
			}
		}
		.list-box{
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-bottom: 4rpx;
			padding: 24rpx;
			background: #fff;
			.left{
				flex-grow: 1;
				overflow: hidden;
				.title{
					color: #333;
					font-size: 30rpx;
					white-space: nowrap;
					text-overflow: ellipsis;
					overflow: hidden;
					line-height: 52rpx;
					.tag{
						display: inline-block;
						padding: 4rpx 10rpx;
						color: #fa436a;
						font-size: 20rpx;
						margin-right: 10rpx;
						background: #fffafb;
						border: 2rpx solid #ffb4c7;
						border-radius: 4rpx;
						line-height: 1;
					}
				}
				.detail{
					margin-top: 10rpx;
					color: #999;
					font-size: 28rpx;
					.name{
						color: #666;
					}
					.tel{
						margin-left: 16rpx;
					}
				}
			}
			.right{
				flex-shrink: 0;
				margin-left: 24rpx;
				.icon{
					color: #999;
					font-size: 40rpx;
				}
			}
		}
	}
	.foot-wrap{
		height: 96rpx;
		.foot-box{
			display: flex;
			justify-content: space-between;
			align-items: center;
			position: fixed;
			left: 0;
			right: 0;
			bottom: 0;
			padding: 0 24rpx;
			height: 100rpx ;
			.btn-add{
				display: block;
				padding: 0 48rpx;
				color: #fff;
				font-size: 30rpx;
				line-height: 76rpx;
				width: 100%;
				height: 76rpx;
				text-align: center;
				background: #ff5000;
				border-radius: 48rpx;
				box-shadow: 0 2rpx 8rpx rgba($color: #ff5000, $alpha: 0.2)
			}
		}
	}
</style>